<?php require_once '../includes/config.php'; ?>
<?php require_once '../head.php'; ?>
<?php session_start(); ?>
<?php confirmed_logged_in(); ?>

<?php

/* 
 * Display the form if $_POST['submit'] is not set
 * Display the fields and options to edit them
 * else
 * process the upload price.php
 */
?>

<?php if(isset($_POST['submit'])): 

    $size = filter_input(INPUT_POST, 'size');
    $price_1 = filter_input(INPUT_POST, 'price_1');
    $blprice_1 = filter_input(INPUT_POST, 'blprice_1');
    $blprice_2 = filter_input(INPUT_POST, 'blprice_2');
    $blprice_4 = filter_input(INPUT_POST, 'blprice_4');

    $result = query("INSERT INTO price_list (`size`, `price_1`, `blprice_1, `blprice_2`, `blprice_4`) VALUES (?, ?, ?, ?)", $size, $price_1, $blprice_1, $blprice_2, $blprice_4);

    //echo "$size | $price_1 | $blprice_1 | $blprice_2 | $blprice_4 ";
    header('Location: http://projects.asigauto.com/admin/prices.php');

     
?>

<?php else: ?>   
    

 <div class="container ">
     <div class="row">
        <form action="prices.php" method="POST" class="admin-form col-lg-12">
            <div class="form-group col-lg-4">
                <label for="size">Големина Мотор</label>            
                <input id="size" class="form-control " type='text' name="size" value="CC">
            </div>
            <div class="form-group col-lg-2">
                <label for="price1">1 внос.</label>
                <input id="price1" class="form-control " type='text' name="price_1" value="$$">
            </div>
            <div class="form-group col-lg-2">
                <label for="blprice1">1 внос.(blacklist)</label>
                <input id="blprice1" class="form-control " type='text' name="blprice_1" value="$$">
            </div>
            <div class="form-group col-lg-2">
                <label for="blprice2">2 внос.(blacklist)</label>
                <input id="blprice2" class="form-control" type='text' name="blprice_2" value="$$">
            </div>
            <div class="form-group col-lg-2">
                <label for="blprice4">4 внос.(blacklist)</label>
                <input id="blprice4" class="form-control" type='text' name="blprice_4" value="$$">
            </div>
            <div class="form-group">
            <button class="btn btn-primary butt" name="submit" value="Запиши">Запиши</button>
            </div>
        </form>

         </div>
    <hr>
    
    <div class="row col-lg-12">
        
            <table class="table table-striped">
                <tr><td>Кубатура (под )</td><td>1 вноска</td><td>1 вноска(bl)</td><td>2 вноски (bl)</td><td>4 вноски(bl)</td></tr>
    
<?php
    /*
     * Display the fields
     */
    
    $price_list = query("SELECT * FROM price_list");    
    $output = '';
    foreach($price_list as $price_row){
        $output .= '<tr>';
        $output .= '<td><form action="update_prices.php" method="POST" class=""><input name="size" value="'. $price_row["size"] . '">';
        $output .= '<input name="id" type="hidden" value="'. $price_row["id"] .'"></td>';
        $output .= '<td><input name="price_1" value="'. $price_row["price_1"] . '"></td>';
        $output .= '<td><input name="blprice_1" value="'. $price_row["blprice_1"] . '"></td>';
        $output .= '<td><input name="blprice_2" value="'. $price_row["blprice_2"] . '"></td>'; 
        $output .= '<td><input name="blprice_4" value="'. $price_row["blprice_4"] . '"></td>'; 
        $output .= '<td><button class="btn btn-primary" name="edit">Промени</button></form></td>';
        $output .= '</tr>';
    }

     echo $output;
?>
        </table>
        </div>
    
<?php endif; ?>